<template>
	<view class="content">
		<view class="text-area">
			<view>蓝牙状态：
				<text v-if="!connectedDevice" class="did">未连接</text>
				<text v-else class="did">已连接： {{ connectedDevice.deviceId }}</text>
			</view>
		</view>
		
		<view class="text-area">
			波形结果：{{ waveResult }}
		</view>
		<view class="text-area">
			瞬时结果：{{ timeResult }}
		</view>
		<view class="text-area measure-area" v-if="measureType == 'time'">
			{{ measuredArr.length ? measuredArr : '' }}
		</view>
		
		<view class="btn-area">
			<button type="primary" size="mini" @click="scanSensor">扫码</button>
			<button type="primary" size="mini" @click="bindSensor">选择</button>
			<button type="primary" size="mini" @click="readTime('wendu')">温度</button>
		</view>
		<view class="btn-area">
			<button type="primary" size="mini" @click="read('zf')">波形--位移</button>
			<button type="primary" size="mini" @click="read('jsd')">波形--加速度</button>
			<button type="primary" size="mini" @click="read('sudu')">波形--速度</button>
		</view>
		<view class="btn-area">
			<button type="primary" size="mini" @click="readTime('zf')">瞬时--位移</button>
			<button type="primary" size="mini" @click="readTime('jsd')">瞬时--加速度</button>
			<button type="primary" size="mini" @click="readTime('sudu')">瞬时--速度</button>
		</view>
		
		<uni-popup ref="wave" type="top" style="top: 30%;height: calc(50% - 150px);">
			<wave-charts :type="waveType" :waveData="waveData" @readWave="readWave" @closePop="closePop"></wave-charts>
		</uni-popup>
	</view>
</template>

<script>
	import waveCharts from './components/waveCharts.vue'
	import buleTooth from './utils/blueTooth.js'
	import { notice } from '@/utils/common.js'
	
	export default {
		mixins: [
			buleTooth 
		],
		components: {
			waveCharts
		},
		data() {
			return {
				connectedDevice: '',
				measuredArr: [],
				waveType: '',
				waveData: [],
				waveResult: '',
				timeResult: '',
				measureType: ''
			}
		},
		onLoad() {
			uni.openBluetoothAdapter()
		},
		methods: {
			bindSensor() {
				let _this = this;
				uni.navigateTo({
					url: './list?connectedDevice=' + JSON.stringify(_this.connectedDevice),
					events: {
						bindSensor(device) {
							_this.connectedDevice = device
							_this.getBLEDeviceServices()
							// _this.stopBluetoothDevicesDiscovery()
						}
					}
				})
			},
			scanSensor() {
				let _this = this
				uni.scanCode({
					onlyFromCamera: true,
					success: (res) => {
						console.log('二维码内容：' + res.result);
						let deviceId = res.result // res.result.split('-')[1]
						let scanDevice = {
							deviceId: deviceId
						}
						_this.connect(scanDevice, 'sacn')
						// _this.getBLEDeviceServices()
						// _this.stopBluetoothDevicesDiscovery()
					}
				});
			},
			readTime(type) {
				this.timeResult = ''
				this.measuredArr = []
				this.measureType = 'time'
				if(type == 'wendu') {
					this.readTemp()
				} else if(type == 'zf') {
					this.readVibration()
				} else if(type == 'jsd') {
					this.readAcceleration()
				} else if(type == 'sudu') {
					this.readSpeed()
				}
				
				setTimeout(()=>{
					this.timeResult = this.handleResult(this.measuredArr)
				}, 4000)
			},
			read(type) {
				this.measureType = 'wave'
				this.waveResult = ''
				this.measuredArr = []
				
				if(!this.connectedDevice) {
					notice.toast("传感器未连接")
					return
				}
				
				this.waveType = type
				this.$refs.wave.open()
			},
			readWave(type) {
				this.waveData = []
				this.measuredArr = []
				if(type == 'zf') {
					this.readVibrationWave()
				} else if(type == 'jsd') {
					this.readAccelerationWave()
				} else if(type == 'sudu') {
					this.readSpeedWave()
				}
				setTimeout(()=>{
					let resultData = this.handleWaveResult(this.measuredArr)
					this.waveData = resultData.dataList.slice(resultData.dataList.length - 257)
					this.waveResult = resultData.dataValue
				}, 4000)
			},
			closePop() {
				this.$refs.wave.close()
			},
			relieveBind() {
				this.connectedDevice = ""
				uni.removeStorageSync('bindSensor')
			}
		}
	}
</script>

<style lang="scss">
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.logo {
		height: 200rpx;
		width: 200rpx;
		margin-top: 200rpx;
		margin-left: auto;
		margin-right: auto;
		margin-bottom: 50rpx;
	}

	.text-area {
		display: flex;
		margin: 10px 0;
	}
	
	.measure-area {
		font-size: 12px;
	}
	
	.btn-area {
		display: flex;
		margin: 10px 0;
		
		button {
			margin: 0 10px;
		}
	}
</style>
